package xin.mashibo.genvictobutest.service;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;

import com.genvict.obusdk.data.CardConsumeRecord;
import com.genvict.obusdk.data.CardInformation;
import com.genvict.obusdk.data.CardOwner;
import com.genvict.obusdk.data.CardTransactionRecord;
import com.genvict.obusdk.data.ServiceStatus;

import java.util.ArrayList;
import java.util.List;

import xin.mashibo.genvictobutest.MyApplication;
import xin.mashibo.genvictobutest.vo.CardConsumeExtend;
import xin.mashibo.genvictobutest.vo.CardTransactionExtend;

import static xin.mashibo.genvictobutest.constant.ServiceConstant.CARDOWNER_KEY;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.CONSUME_LIST_KEY;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.KEYINDEX;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.MSG_CONNECT;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.MSG_DISCONNECT;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.MSG_MAC1;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.MSG_MAC2;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.MSG_READCARD;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.MSG_READCONSUME;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.MSG_READDEVICE;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.MSG_READOWNER;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.MSG_TRANSACTION;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.NONE_READCARD;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.PING_10;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.PING_40;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.PROCTYPE;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.READCARD_KEY;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.TERMINALNO;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.TRANSACTION_KEY;
import static xin.mashibo.genvictobutest.constant.ServiceConstant.VERSION_40;


/**
 * 蓝牙OBU操作服务
 *
 * @author MaShibo
 *         on 2018/3/20
 */
public class ObuOptService {

    private static final String TAG = "ObuOptService";
    public static final int MAX_NUMBER = 2;

    private Handler handler;

    private CardInformation cardinfo;


    public ObuOptService(Handler handler) {
        this.handler = handler;
    }

    /**
     * 连接蓝牙
     */
    public void connect() {
        new Thread() {
            @Override
            public void run() {
                ServiceStatus status = MyApplication.obuInterface.connectDevice();
                Message msg = new Message();
                msg.what = MSG_CONNECT;
                msg.obj = status;
                handler.sendMessage(msg);
            }
        }.start();
    }

    /**
     * 断开连接
     */
    public void disConnect() {
        new Thread() {
            @Override
            public void run() {
                MyApplication.obuInterface.disconnectDevice();
                Message msg = new Message();
                msg.what = MSG_DISCONNECT;
                ServiceStatus status = new ServiceStatus();
                status.setStatus(ServiceStatus.RESULT_OK);
                msg.obj = status;
                handler.sendMessage(msg);
            }
        }.start();
    }

    /**
     * 读卡片信息
     */
    public void readCard() {
        new Thread() {
            @Override
            public void run() {
                CardInformation cardInfomation = new CardInformation();
                ServiceStatus status = MyApplication.obuInterface.getCardInformation(cardInfomation);
                cardinfo = cardInfomation;
                Message msg = new Message();
                msg.what = MSG_READCARD;
                msg.obj = status;
                Bundle bundle = new Bundle();
                bundle.putParcelable(READCARD_KEY, cardInfomation);
                msg.setData(bundle);
                handler.sendMessage(msg);
            }
        }.start();
    }

    /**
     * 读取卡片所有人信息
     */
    public void readCardOwner() {
        new Thread() {
            @Override
            public void run() {
                Message msg = new Message();
                ServiceStatus status = new ServiceStatus();
                if (cardinfo == null) {
                    status.setStatus(NONE_READCARD);
                } else {
                    String pinCode = VERSION_40.equals(cardinfo.getCardVersion()) ? PING_40 : PING_10;
                    CardOwner cardOwner = new CardOwner();
                    status = MyApplication.obuInterface.readCardOwnerInfo(pinCode, cardOwner);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(CARDOWNER_KEY, cardOwner);
                    msg.setData(bundle);
                    cardinfo = null;
                }
                msg.what = MSG_READOWNER;
                msg.obj = status;
                handler.sendMessage(msg);
            }
        }.start();
    }

    /**
     * 读取卡片消费记录
     */
    public void readConsume() {
        new Thread() {
            @Override
            public void run() {
                List<CardConsumeRecord> cardRecordList = new ArrayList<>(MAX_NUMBER);
                ServiceStatus status = MyApplication.obuInterface.readCardConsumeRecord(MAX_NUMBER, cardRecordList);
                Message msg = new Message();
                msg.what = MSG_READCONSUME;
                msg.obj = status;
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList(CONSUME_LIST_KEY, convertConsumeList(cardRecordList));
                msg.setData(bundle);
                handler.sendMessage(msg);
            }
        }.start();
    }

    /**
     * 读取设备信息
     */
    public void readDevice() {
        new Thread() {
            @Override
            public void run() {
                ServiceStatus status = MyApplication.obuInterface.getDeviceInformation();
                Message msg = new Message();
                msg.what = MSG_READDEVICE;
                msg.obj = status;
                handler.sendMessage(msg);
            }
        }.start();
    }

    /**
     * 读取卡片交易记录
     */
    public void readTransaction() {
        new Thread() {
            @Override
            public void run() {
                Message msg = new Message();
                ServiceStatus status = new ServiceStatus();
                if (cardinfo == null) {
                    status.setStatus(NONE_READCARD);
                } else {
                    String pinCode = VERSION_40.equals(cardinfo.getCardVersion()) ? PING_40 : PING_10;
                    List<CardTransactionRecord> cardRecordList = new ArrayList<>(MAX_NUMBER);
                    status = MyApplication.obuInterface.readCardTransactionRecord(pinCode, MAX_NUMBER, cardRecordList);
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList(TRANSACTION_KEY, convertTransactionList(cardRecordList));
                    msg.setData(bundle);
                    cardinfo = null;
                }
                msg.what = MSG_TRANSACTION;
                msg.obj = status;
                handler.sendMessage(msg);
            }
        }.start();
    }

    /**
     * 读取MAC1相关信息
     *
     * @param credit 充值金额
     */
    public void readMac1(final int credit) {
        new Thread() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.what = MSG_MAC1;
                ServiceStatus status = new ServiceStatus();
                if (cardinfo == null) {
                    status.setStatus(NONE_READCARD);
                } else {
                    String pinCode = VERSION_40.equals(cardinfo.getCardVersion()) ? PING_40 : PING_10;
                    status = MyApplication.obuInterface.loadCreditGetMac1(cardinfo.getCardId(), credit, TERMINALNO, pinCode, PROCTYPE, KEYINDEX);
                    cardinfo = null;
                }
                msg.obj = status;
                handler.sendMessage(msg);
            }
        }.start();
    }

    /**
     * 圈存操作
     *
     * @param mac2 yyyyMMddHHmmss + 从加密机获取的MAC2
     */
    public void writeCard(final String mac2) {
        new Thread() {
            @Override
            public void run() {
                ServiceStatus status = MyApplication.obuInterface.loadCreditWriteCard(mac2);
                Message msg = new Message();
                msg.what = MSG_MAC2;
                msg.obj = status;
                handler.sendMessage(msg);
            }
        }.start();
    }

    /**
     * 转换消费记录列表格式
     *
     * @param consumeList 消费记录列表
     * @return 消费记录列表
     */
    private ArrayList<? extends Parcelable> convertConsumeList(List<CardConsumeRecord> consumeList) {
        ArrayList<CardConsumeExtend> extendList = new ArrayList<>(consumeList.size());
        for (CardConsumeRecord cardConsumeRecord : consumeList) {
            extendList.add(new CardConsumeExtend(cardConsumeRecord));
        }
        return extendList;
    }

    /**
     * 转换交易记录格式
     *
     * @param transactionList 交易记录列表
     * @return 交易记录列表
     */
    private ArrayList<? extends Parcelable> convertTransactionList(List<CardTransactionRecord> transactionList) {
        ArrayList<CardTransactionExtend> extendList = new ArrayList<>(transactionList.size());
        for (CardTransactionRecord cardTransactionRecord : transactionList) {
            extendList.add(new CardTransactionExtend(cardTransactionRecord));
        }
        return extendList;
    }

}
